<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:43
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;
use think\Loader;

class Admin extends AdminBase
{
    /**
     * 管理员列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $where      = '1 = 1';
            $title      = '';
            $level      = 0;
            $date_start = time();
            $date_end   = $date_start;
            if (request()->isPost())
            {
                $title      = input('param.title');
                $level      = intval(input('param.level'));
                $daterange  = input('param.daterange');
                $date_arr   = explode('-', $daterange);
                $date_start = strtotime(trim($date_arr[0]));
                $date_end   = strtotime(trim($date_arr[1]));
                if (!empty($title))
                {
                    $where .= ' and a.username like "%'.$title.'%"';
                }
                if (!empty($level))
                {
                    $where .= ' and a.level = '.$level;
                }
                if (($date_end-$date_start) > 0)
                {
                    $where .= ' and a.create_time between '.$date_start.' and '.$date_end;
                }
            }
            $list = Db::name('admin')
                ->alias('a')
                ->field('a.id, a.username, a.level, a.create_time, a.status, j.title')
                ->join('__ADMIN_ROLE__ j', 'a.level = j.id')
                ->order('a.create_time desc')
                ->where($where)
                ->paginate(20);
            $pages = $list->render();
            $juriList = Db::name('admin_role')->field('id, title')->select();
            return view('index', [
                'juriList'=>$juriList,
                'title'=>$title,
                'level'=>$level,
                'date_start'=>$date_start,
                'date_end'=>$date_end,
                'list'=>$list,
                'pages'=>$pages
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function add()
    {
        $chk = check_juri(request()->controller(), 'add', $this->_juri);
        if ($chk['status'])
        {
            $juriList = Db::name('admin_role')->field('id, title')->select();
            return view('add', [
                'juriList'=>$juriList
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function doAdd()
    {
        if (request()->isPost())
        {
            $password            = input('param.password');
            $confirm_pass        = input('param.confirm_pass');
            if ($password != $confirm_pass)
            {
                return ['status'=>false, 'msg'=>'两次输入的密码不一致'];
            }
            $data                = [];
            $data['username']    = input('param.username');
            $data['password']    = get_hash_pass($password);
            $data['level']       = intval(input('param.level'));
            $data['status']      = intval(input('param.status'));
            $data['create_time'] = time();
            $data['update_time'] = time();
            $res = Db::name('admin')->insertGetId($data);
            if ($res)
            {
                return ['status'=>true, 'msg'=>url('index')];
            } else {
                return ['status'=>false, 'msg'=>'管理员添加失败'];;
            }
        }
    }

    /**
     * 编辑管理员信息
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $chk = check_juri(request()->controller(), 'edit', $this->_juri);
        if ($chk['status'])
        {
            $id   = intval(input('param.id'));
            $info = Db::name('admin')
                ->field('id, username, status, level')
                ->where('id', $id)
                ->find();
            $juriList = Db::name('admin_role')->field('id, title')->select();
            return view('edit', [
                'juriList' => $juriList,
                'info'=>$info
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    /**
     * 执行修改信息
     * @return bool|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function doEdit()
    {
        if (request()->isPost())
        {
            $id                  = intval(input('param.id'));
            $password            = input('param.password');
            $confirm_pass        = input('param.confirm_pass');
            $data                = [];
            if (!empty($password) && !empty($confirm_pass))
            {
                if ($password != $confirm_pass)
                {
                    return ['status'=>false, 'msg'=>'两次输入的密码不一致'];
                }
                $data['password']    = get_hash_pass($password);
            }
            $data['level']       = intval(input('param.level'));
            $data['status']      = intval(input('param.status'));
            $data['update_time'] = time();
            $res = Db::name('admin')->where('id', $id)->update($data);
            if ($res)
            {
                return ['status'=>true, 'msg'=>url('index')];
            } else {
                return ['status'=>false, 'msg'=>'编辑失败'];
            }
        }
    }

    public function del()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id  = intval(input('param.id'));
                $res = Db::name('admin')->where('id', $id)->delete();
                return $res ? ['status'=>true, 'msg'=>'删除成功'] : ['status'=>false, 'msg'=>'删除失败'];
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
}